package com.gf.framework.common.utils.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
 * @author gaofei
 * @date 2022/6/12 19:37
 */
public interface GfMap<K, V> extends Map<K, V> {
    default GfMap<K, V> of(K k, V v) {
        put(k, v);
        return this;
    }

    /**
     * k 和 v不能是null
     * @param k
     * @param v
     * @return
     */
    default GfMap<K, V> ofNotNull(K k, V v) {
        if (k == null || v == null) {
            return this;
        }
        put(k, v);
        return this;
    }

    /**
     * 获取多个值
     * @param keys
     * @return
     */
    default List<V> getMulti(K... keys) {
        if (keys == null) {
            return new ArrayList<>(0);
        }

        List<V> vList = new ArrayList<>(keys.length);
        for (K key : keys) {
            vList.add(getOrDefault(key, null));
        }
        return vList;
    }

    default GfMap<K, V> putNotNull(K k, V v) {
        return ofNotNull(k, v);
    }

    default <T> GfMap<K, V> putAll(Collection<T> objects, KVConsumer<K, V, T> consumer) {
        for (T data: objects) {
            put(consumer.key(data), consumer.value(data));
        }
        return this;
    }

    default <T> GfMap<K, V> putAll(T[] objects, KVConsumer<K, V, T> consumer) {
        for (T data: objects) {
            put(consumer.key(data), consumer.value(data));
        }
        return this;
    }
}
